cleaned up, needs more comments