fixed bugs, streamlined control flow, removed busy loops