Cleaned up code; func