Cleaner code and minor bugfix