restructure code a little