Cleaned up and splitted code into several classes