Many things fixed, everything tested, still some bugs, noted in comments.