tested code, fixed semantics and variables