Added another subroutine to reduce repeated code. Fixed a bug I created in the last commit