Fixed a small bug and made code more compact