Cleaned up code. Still modifying because of infinite loop at the end...