restuctured and reused existing functions.