If you are free feeding, stop. Feed your puppy 2-3 times each day and the elimination pattern will be more predictable. Give water freely at meal times, but just leave a little to lick up (or an ice cube) in the bowl otherwise.
When you cannot have both eyes and 100% attention on the puppy put him in the crate. If you leave him to wander on his own and he has an accident you are responsible for setting him up to fail. As he gets more reliable, you can tether him to you with a leash so that he does ot wander off and you can watch for sniffing/circling.
If these strategies don't solve the issues be sure to mention it to the vet at the next vaccination appointment or make an appointment to be seen. Puppies can have problems with urination frequency if they have parasites, a UTI, bladder stones, ... Bladder stones are more common in certain breeds like corgis.
A 4 month old puppy does not have the capability to control his bladder or bowels yet. It is up to you to take him out often enough and supervise well enough to avoid accidents. If he's still going in the house after going out every 2 hrs, then you need to take him out every hour, or every 45 minutes, or 30 minutes. Keep shortening the time between bathroom trips until he is no longer having accidents. Over time, you can gradually increase the time between trips out. You should be disciplining yourself if he goes in the house, not him. It's not his fault. He can't control it. You can control how many trips out he's getting and how much supervision he's getting. I seriously doubt he was fully housebroken in his previous home.
In between bathroom trips, you need to watch him with your undivided attention, or crate him. If you're watching him, you can grab him when he starts to sniff/circle/squat and run outside.
When he does go outside, reward him! Reward him outside when he's done going; don't wait to get back into the apartment (or he'll think he's getting rewarded for going in the apartment).