Major refactoring, code styling