several improvements and proper code