Added support for dynamic number of terms and fixed basic coding style