Lots of changes for cleaner code