reorginized the code a little bit...