Externalised the functions used into a separate file for better readability and trimmed the unneeded ones