broke up some functions into smaller parts, improving clarity