Cleaned up redundant code: added function for it.