Refactor code and split things into classes