Refactored quite a bit