Refactor and remove lots of redundancy.