added various functions and optimized solutions