Refactored the code a little bit to make it more modular. Could still use a little work.