Turned into function - still buggy