Fixes and improvements to the Racket code

I've fixed some bugs and made some improvements, but I tried to do so while keeping it close to the original.