Bugfixes

Made some modifications. Please feel free to grab the code.