cleaned up a bit the code