remove redundant use of A register,
make a loop a bit safer, add some comments