General cleaning of code, small bug fixes