Cleaned up code, more efficient