multiple bug fixesm and refactoring