Cleanup code, gather common functions together