Added export function and did some improvements.