Clean up code style and completely reorganise