Code cleaning, bug fixes