cleaning up logic and edge cases