Cleaned up function