#534. Fixed error, minor changes in logic.