Cleaned up code and fixed lookup functions