refactored a bit to accomodate more code