more accurate memory management
added test for error message