Refactor. Bugs still exist