Finished modularising existing code + various improvements