Cleaned up model and separated into functions