code partially cleaned up