finished a lot of the remaining logic