Somewhat big overhaul of code. Not sure if it works.