Fixed #U4-5373

Also cleaned up code a little