fixed issue #41 and some small bug fixes