General refactoring, improved formatting