Code overhauled and refactored