Updated and extended

Code rewrite
Added variable expansion