Refactored some funky logic