Cleaned up code to use more suitable functions