updated for latest logic and comments