upgraded code. small bug fixes