Cleaned up code a bit and made it more efficient.