Should be working fine, haven't check for minor bugs, a bit confusing at the loop