updated with minor changes to logic.