refactoring,error handling,fix return values