Refactored code, tinkered around with the free space, but feel free to edit the current implementation