reorganized code
fixed somes to make previous commit work