Much cleaner version of the code