Refactored, split everything in more source files for readability.