Lots of improvements on code organization