added several features and rewrote a bit