Cleaned up code and added some useful functions
Fixed merge conflicts