Finished refactoring. Score and cleanup at the end still missing.