3AM end of code sesh. Slightly modified and working, thanks to @rafkhan. I'm sleepy