Added endless mode

Thanks to PeterCxy for much of the code cleanup.
Thanks to Allen Wang for finding a bug.