Minor edits to logic. Still needs more error condition checking.