Re-arranged code to make it more usable