Replaced obsolete methods
State machine -> simple while loop