refatoring and bug fixing in firmmash