Bug Fix

Bug Fixed.
Seems to work perfectly. I left some debugging code for future testing.