functionality exracted from controller