Moved logic across to main to maintain functionality as before